package com.glsc.ngateway.opmanage.controller.deposit;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDayStartTrial;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDaySummary;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.DepositoryBankDto;
import com.glsc.ngateway.opmanage.dto.DepositoryDayStartTrialDto;
import com.glsc.ngateway.opmanage.dto.DepositoryDto;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryDayStartTrialService;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryDaySummaryService;
import com.glsc.ngateway.opmanage.utils.DateUtils;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 保证金每日试算（新）日初
 */
@RestController
@RequestMapping("/api/depository/dayStartTrial")
@Slf4j
public class DepositoryDayStartTrialController {

    @Resource
    private UserCache userCache;

    @Resource
    private DepositoryDayStartTrialService depositoryDayStartTrialService;

    @Resource
    private DepositoryDaySummaryService depositoryDaySummaryService;

    /**
     * 创建今日数据
     *
     * @return
     */
    @GetMapping("/create")
    public PlatformResponse<?> createDate() {
        depositoryDayStartTrialService.createDate();
        return PlatformResponse.success();
    }

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DepositoryDayStartTrial>> page(@RequestBody DepositoryDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryDayStartTrial> page = depositoryDayStartTrialService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    /**
     * 保证金试算日终汇总数据
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/sumAmount")
    public PlatformResponse<DepositoryDayStartTrialDto> sumAmount(@RequestBody DepositoryDto dto, HttpServletRequest request) {
        DepositoryDayStartTrialDto res = DepositoryDayStartTrialDto.builder().adjustValue(BigDecimal.ZERO)
                .counterBalance(BigDecimal.ZERO).bankBalance(BigDecimal.ZERO).build();

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryDayStartTrial> page = depositoryDayStartTrialService.page(1, 1000, param);
        if (Objects.nonNull(page) && CollectionUtils.isNotEmpty(page.getContent())) {
            for (DepositoryDayStartTrial trial : page.getContent()) {
                if (Objects.nonNull(trial.getAdjustValue())) {
                    res.setAdjustValue(res.getAdjustValue().add(trial.getAdjustValue()));
                }

                if (Objects.nonNull(trial.getBankBalance())) {
                    res.setBankBalance(res.getBankBalance().add(trial.getBankBalance()));
                }

                if (Objects.nonNull(trial.getCounterBalance())) {
                    res.setCounterBalance(res.getCounterBalance().add(trial.getCounterBalance()));
                }

            }
        }

        DepositoryDaySummary summary = depositoryDaySummaryService.findByDate(DateUtils.getNowDateString());
        if (Objects.nonNull(summary)) {
            res.setAdjustInTotal(summary.getAdjustInTotal());
            res.setAdjustOutTotal(summary.getAdjustOutTotal());
            res.setPaymentTotal(summary.getPaymentTotal());
            res.setFoundationTotal(summary.getFoundationTotal());
            res.setReceiveTotal(summary.getReceiveTotal());
            res.setPaddingAmount(summary.getPaddingAmount());

        }

        return PlatformResponse.successData(res);
    }


    /**
     * 保存基金总额信息
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/saveFoundationTotal")
    public PlatformResponse<DepositoryDaySummary> saveFoundationTotal(@RequestBody DepositoryDayStartTrialDto dto, HttpServletRequest request) {

        if (Objects.isNull(dto)) {
            return PlatformResponse.failedMsg("基金金额不可为空");
        }

        DepositoryDaySummary summary = depositoryDaySummaryService.saveFoundationTotal(dto);

        return PlatformResponse.successData(summary);
    }


    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<DepositoryDayStartTrial> findTemplateById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(depositoryDayStartTrialService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> saveTemplate(@RequestBody DepositoryDayStartTrial obj) {
        depositoryDayStartTrialService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据id删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> deleteTemplate(@RequestBody DepositoryDayStartTrial obj) {
        depositoryDayStartTrialService.deleteById(obj.getId());
        return PlatformResponse.success();
    }

    /**
     * 批量删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/deleteByIds")
    public PlatformResponse<?> deleteTemplate(@RequestBody DepositoryDto obj) {

        if (Objects.isNull(obj)) {
            return PlatformResponse.failedMsg("删除数据不可为空");
        }
        depositoryDayStartTrialService.deleteByIds(obj.getIds());
        return PlatformResponse.success();
    }


}
